package com.cnswhy.cloud.server.system.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import com.cnswhy.cloud.common.core.mybatis.conditions.Wraps;
import com.cnswhy.cloud.server.system.dao.SystemOrgMapper;
import com.cnswhy.cloud.server.system.entity.SystemOrg;
import com.cnswhy.cloud.server.system.service.SystemOrgService;
import com.cnswhy.cloud.common.core.base.service.SuperServiceImpl;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务实现类
 * 组织
 * </p>
 *
 * @author cnswhy
 * @date 2020-10-13
 */
@Slf4j
@Service

public class SystemOrgServiceImpl extends SuperServiceImpl<SystemOrgMapper, SystemOrg> implements SystemOrgService {

    @Override
    public List<SystemOrg> findChildren(List<Long> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        // MySQL 全文索引
        String applySql = String.format(" MATCH(tree_path) AGAINST('%s' IN BOOLEAN MODE) ", StringUtils.join(ids, " "));

        return super.list(Wraps.<SystemOrg>lbQ().in(SystemOrg::getId, ids).or(query -> query.apply(applySql)));
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean remove(List<Long> ids) {
        List<SystemOrg> list = this.findChildren(ids);
        List<Long> idList = list.stream().mapToLong(SystemOrg::getId).boxed().collect(Collectors.toList());

        boolean bool = !idList.isEmpty() ? super.removeByIds(idList) : true;

        // 删除自定义类型的数据权限范围
        //roleOrgService.remove(Wraps.<RoleOrg>lbQ().in(RoleOrg::getOrgId, idList));
        return bool;
    }
}
